const HtmlWebpackPlugin = require('html-webpack-plugin'); // 自动引用资源
const MiniCssExtractPlugin  = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');

const { resolve } = require('path')
const htmlPublicPath = resolve(__dirname, 'build')

module.exports = {
  entry: './src/js/index.js',
  output: {
    filename: 'js/buit.js',
    path: htmlPublicPath,
    publicPath: './',
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          // 作用： 提取js中的css成单独一个文件(默认main.css)
          MiniCssExtractPlugin.loader,
          // 减少 打包后的js体积，提高js运行效率
          
          // 将css 文件整合到js文件中
          'css-loader',

          /* 有2中写法
            css 兼容性处理： postcss --> postcss-loader postcss-preset-env

              postcss-loader
              postcss-preset-env 帮组postcss找到package.json中 browserslist 里面的配置，
                                 通过配置加载指定的css兼容性样式
           */

          // 1.使用 loader的默认配置
          //  'postcss-loader',

          // 2 修改loader配置
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  [
                    "postcss-preset-env",
                    {
                      // Options
                    },
                  ],
                ]
              }
            }
          }

          
        ],
      }
    ]
  },
  optimization: {
    minimize: true,
    minimizer: [
      // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
      // `...`
      new CssMinimizerPlugin(),
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    }),
    new MiniCssExtractPlugin({
      // 对 css 文件，重命名
      // filename: 'css/built.css',

      // 默认main.css
      filename: 'css/[name].[hash:7].css',
      chunkFilename: 'css/[id].[hash:7].css',
    }),
  ]
}